import { Controller, UseGuards, Body, Post, Req } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { DemandDto } from './dto/demand.dto';
import { ApiUseTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { RequestUser } from '@/common/interfaces/request.interface';
import { ApiResult } from '@/common/interceptors/result.interceptor';
import { DemandService } from './demand.service';
import { TeamService } from '@/team/team.service';

@ApiUseTags('需求模块')
@ApiBearerAuth()
@Controller('demand')
@UseGuards(AuthGuard())
export class DemandController {
  constructor(
    private readonly demandService: DemandService,
    private readonly teamService: TeamService
  ) {}

  @Post('list')
  @ApiOperation({
    title: '查询需求列表',
    description: '需求菜单中使用,用来查询需求列表'
  })
  async searchDemandList(@Req() req: RequestUser, @Body() param: DemandDto) {
    const user = req.user;
    const team = await this.teamService.findTeamForActive(user._id);
    const data = await this.demandService.getDeamndList(user._id, team._id);
    return new ApiResult(data);
  }

  @Post('create-demand')
  @ApiOperation({
    title: '新建需求',
    description: '创建一个需求'
  })
  async createDemand(@Req() req: RequestUser, @Body() demand: DemandDto) {
    const user = req.user;
    const team = await this.teamService.findTeamForActive(user._id);
    const data = {
      demandName: demand.demandName,
      demandLink: demand.demandLink,
      webhook: demand.webhook,
      teamId: team._id,
      userId: user._id
    };
    await this.demandService.createDemand(data);
    return new ApiResult('');
  }
}
